//
//  YsUploadManager+Private.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/3/6.
//

#import "YsUploadManager+Private.h"
#import "NSString+YscCloudFileTools.h"
@implementation YsUploadManager (Private)
- (void)_uploadWithPath:(NSString *)uploadPath
              withPara:(NSString *)para
            withMethod:(NSString *)requestMethod
 withLastPathComponent:(NSString *)lastPathComponent
        withIsMoveFile:(BOOL)isMoveFile
{
    SemaphoreBegin;
    NSParameterAssert(uploadPath);
    NSParameterAssert(lastPathComponent);

    YsDocUploadModel *uploadModel = [[YsDocUploadModel alloc] initWithProductUploadModel:YES];
   
    if (isMoveFile) {
        NSString *srcPath =   [YsCloudFolderManager getPathToCacheViedoWithAccountCode:kAccountCode withlastPathComponent:lastPathComponent withfileName:[[uploadPath.lastPathComponent componentsSeparatedByString:@"."] objectOrNilAtIndex:0]];
           /// 文件移动
           [YsFileUtil moveItemAtPath:uploadPath toPath:srcPath];
           uploadModel.filePath = srcPath;
    }else{
        uploadModel.filePath = uploadPath;
    }
    uploadModel.lastPathComponent = lastPathComponent;
    uploadModel.fileName = [[uploadPath.lastPathComponent componentsSeparatedByString:@"."] objectOrNilAtIndex:0];
    int64_t length = [YsFileUtil fileSizeAtPath:uploadModel.filePath];
    NSInteger count = length / (kSuperUploadBlockSize);
    NSInteger blockCount = length % (kSuperUploadBlockSize) == 0 ? count: count + 1;
    if (blockCount == 0) return;
    uploadModel.totalCount = blockCount;
    uploadModel.totalSize = length;
    uploadModel.fileHash = [YsFileUtil readDataWithfilePath:uploadModel.filePath].md5String;
    uploadModel.uploadStatus = 2;
    uploadModel.accountCode = kAccountCode;
    uploadModel.createTime = NSString.currentTimeStr;
    NSLog(@"%@",uploadModel.fileHash);
    [self.modelArray addObject:uploadModel];
    NSLog(@"上传的数据源-----：%@",self.modelArray);
    NSPredicate *query = [NSPredicate predicateWithFormat:@"accountCode == %@",kAccountCode];
      YsCloudFileManageSDKRealm *sdkRealm = (YsCloudFileManageSDKRealm*) [YsCloudFileRealmManager queryObjsWithPredicate:query targetClass:YsCloudFileManageSDKRealm.class].lastObject;
      [YsCloudFileRealmManager updateObj:^{
        [sdkRealm.uploadReamModelArray addObject:uploadModel.fileUploadReamModel];
      }];
    SemaphoreEnd;
}

- (instancetype)init
{
    self = [super init];
    if (self) {        
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:NSString.backgroundSessionIdentifier];
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
        // 设置超时时间
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 1000.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html", nil];
        self.manager = manager;
    }
    return self;
}
@end
